<template>
  <div>
    <!--筛查-->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="仓库名称">
        <el-input v-model="queryParams.repositoryName" placeholder="仓库名称"></el-input>
      </el-form-item>
      <el-form-item label="仓库代号">
        <el-input v-model="queryParams.repositoryCode" placeholder="仓库代号"></el-input>
      </el-form-item>
      <el-form-item label="启动时间">
        <el-date-picker
            v-model="StartDate"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="仓库状态">
        <el-radio v-model="queryParams.repositoryState" placeholder="仓库状态" label="1">正常</el-radio>
        <el-radio v-model="queryParams.repositoryState" placeholder="仓库状态" label="0">停用</el-radio>
        <el-radio v-model="queryParams.repositoryState" placeholder="仓库状态" label="">全部</el-radio>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-plus" size="mini" @click="query">查询
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-plus" size="mini" @click="reSet">清除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addHandler()">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" @click="deleteHandler()" size="mini">删除</el-button>
      </el-col>
    </el-row>
    <br>
    <!--查询数据-->
    <el-table
        :data="tableData"
        stripe v-loading="loading"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column prop="repositoryId" label="仓库主键" width="180"></el-table-column>
      <el-table-column prop="repositoryName" label="仓库名称" width="180"></el-table-column>
      <el-table-column prop="repositoryCode" label="仓库代号"></el-table-column>
      <el-table-column prop="repositoryPhone" label="联系电话"></el-table-column>
      <el-table-column prop="repositoryState" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.repositoryState==0" type="danger">停用</el-tag>
          <el-tag v-else-if="scope.row.repositoryState==1" type="success">正常</el-tag>
          <el-tag v-else type="info">不清楚</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="repositoryAddress" label="仓库地址"></el-table-column>
      <el-table-column prop="repositoryStartDate" :formatter="formatDate" label="启动时间">
      </el-table-column>
      <el-table-column class="caoZuo" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="editHandler(scope.row.repositoryId)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteHandler(scope.row.repositoryId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 50, 100]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!--弹窗页-->
    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="30%"
        append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库名称" prop="repositoryName">
          <el-input v-model="form.repositoryName"></el-input>
        </el-form-item>
        <el-form-item label="仓库代号" prop="repositoryCode">
          <el-input v-model="form.repositoryCode"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="repositoryPhone">
          <el-input v-model="form.repositoryPhone"></el-input>
        </el-form-item>
        <el-form-item label="仓库状态" prop="repositoryState">
          <el-radio-group v-model="form.repositoryState">
            <el-radio :label="0">停用</el-radio>
            <el-radio :label="1">可用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="仓库地址" prop="repositoryAddress">
          <el-input type="textarea" v-model="form.repositoryAddress"></el-input>
        </el-form-item>
        <el-form-item label="活动时间" prop="repositoryStartDate">
          <el-date-picker type="date" placeholder="选择日期" v-model="form.repositoryStartDate"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {editPage,editSave,addSave,deleteIds,getPage} from "@/api/demo/RepositoryInfo.js"

export default {
  data() {
    return {
      total: 0,
      loading: false,
      StartDate: [],
      title: '',
      //用于删除的id集合
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repositoryName: '',
        repositoryCode: '',
        repositoryState: null,
        repositoryStartDateFrom: null,
        repositoryStartDateTo: null
      },
      form: {
        repositoryId:null,
        repositoryName: null,
        repositoryCode: null,
        repositoryPhone: null,
        repositoryState: null,
        repositoryAddress: null,
        repositoryStartDate: null,
      },
      rules: {
        repositoryName: [
          {required: true, message: '请输入仓库名称', trigger: 'blur'},
          {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
        ],
        repositoryCode: [
          {required: true, message: '请输入仓库代号', trigger: 'blur'}
        ],
        repositoryAddress: [
          {min: 0, max: 500, message: '长度在 0 到 500 个字符', trigger: 'blur'}
        ],
      },
      dialogVisible: false,
      formLabelWidth: '120px',
      tableData: [],
    }
  },
  created() {
    this.getList()

  },
  methods: {
    /**
     * 获取数据
     */
    getList() {
      this.loading = true
      // 写请求
      getPage(this.queryParams).then(resp => {
        // console.log(resp.data.rows)
        this.tableData = resp.rows
        this.loading = false
        this.total = resp.total
      })
    },
    /**
     * 清空表单
     */
    resetForm(){
      this.form = {
        repositoryId:null,
        repositoryName: null,
        repositoryCode: null,
        repositoryPhone: null,
        repositoryState: 1,
        repositoryAddress: null,
        repositoryStartDate: null,
      }
    },
    /**
     * 打开添加 对话框
     */
    addHandler() {
      this.resetForm()
      this.title = '添加信息'
      this.dialogVisible = true
    },
    /**
     * 打开修改 对话框
     */
    editHandler(id) {
      console.log(id)
      editPage(id).then(resp => {
        // console.log(resp)
        this.form = resp
        this.title = '修改信息'
        this.dialogVisible = true
      })
    },
    /**
     * 添加保存处理
     */
    onSubmit() {
      // 通过选择器找到form表单进行处理
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //通过了校验
          if (this.form.repositoryId) {
            // 修改
            // axios处理传值进行添加处理
            editSave(this.form).then(resp => {
              //判断成功还是失败,给予明确提示
              if (resp.code == 200) {
                //弹出成功消息
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                // 关闭显示窗
                this.dialogVisible = false
                // 刷新页面
                this.getList()
              } else {
                // 弹出失败消息
                this.$message.error('保存失败');
              }
            })
          }else{
            // 添加
            // axios处理传值进行添加处理
            addSave(this.form).then(resp => {
              //判断成功还是失败,给予明确提示
              if (resp.code == 200) {
                //弹出成功消息
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                // 关闭显示窗
                this.dialogVisible = false
                // 刷新页面
                this.getList()
              } else {
                // 弹出失败消息
                this.$message.error('保存失败');
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }


      });
    },
    /**
     * 删除处理
     * @param id
     */
    deleteHandler(id) {
      // console.log(id)
      //判断传入的是单条操作还是多选操作
      let idss = id || this.ids.join(',')
      if (!idss){
        this.$message({
          message: '请选择要删除的数据',
          type: 'warning'
        });
        return
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // axios处理传值进行删除处理
       deleteIds(idss).then(resp => {
          //判断成功还是失败
          if (resp.code == 200) {
            //弹出成功消息
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            // 刷新页面
            this.getList()
          } else {
            // 弹出失败消息
            this.$message.error('删除失败');
          }
        }).catch(err => {
          // 发生异常时,弹出异常消息
          this.$message.error('删除异常');
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    /**
     *     多选 选中处理
     */
    handleSelectionChange(val) {
      // console.log(val)
      this.ids = val.map(item => item.repositoryId)
      console.log(this.ids)
    },
    /**
     * 时间格式化
     * @param row
     * @param column
     * @param cellValve
     * @param index
     * @returns {string}
     */
    formatDate(row, column, cellValve, index) {
      // console.log(row, column,cellValve,index)
      let date = new Date(cellValve);
      let o = {
        'y': date.getFullYear(),
        'm': (date.getMonth() + 1).toString().padStart(2, '0'),
        'd': date.getDate().toString().padStart(2, '0'),
        'h': date.getHours().toString().padStart(2, '0'),
        'i': date.getMinutes().toString().padStart(2, '0'),
        's': date.getSeconds().toString().padStart(2, '0')
      };
      return o.y + "-" + o.m + "-" + o.d
    },
    /**
     * 分页处理 显示条数改变事件
     * @param val
     */
    handleSizeChange(val) {
      // console.log(val)
      this.queryParams.pageSize = val
      this.getList()
    },
    /**
     * 分页处理 当前页改变事件
     * @param val
     */
    handleCurrentChange(val) {
      // console.log(val)
      this.queryParams.pageNum = val
      this.getList()
    },
    /**
     * 查询处理
     */
    query() {
      this.queryParams.repositoryStartDateFrom = this.StartDate[0]
      this.queryParams.repositoryStartDateTo = this.StartDate[1]
      this.getList()
    },
    /**
     * 重置处理
     */
    reSet() {
      this.queryParams.repositoryName = ''
      this.queryParams.repositoryCode = ''
      this.queryParams.repositoryState = null
      this.queryParams.repositoryStartDateFrom = null
      this.queryParams.repositoryStartDateTo = null
      this.StartDate = []
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
    }
  }
}
</script>

<style scoped lang="scss">
.el-form {
  margin: 20px auto;
  width: 90%;
  text-align: left;
}
</style>
